package com.ruoyi.stock.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.stock.domain.StockBasic;
import com.ruoyi.stock.domain.StockNow;
import com.ruoyi.stock.service.IStockBasicService;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 股票实时数据信息Controller
 *
 * @author ruoyi
 * @date 2021-10-22
 */
@RestController
@RequestMapping("/stock/time")
public class StockTimeController extends BaseController {

  @Autowired private IStockBasicService iStockBasicService;

  private static final String URL = "https://hq.sinajs.cn/list=";

  /** 查询股票实时数据 */
  @PreAuthorize("@ss.hasPermi('stock:time:info')")
  @GetMapping("/info")
  public StockNow list(String stockCode) {
    List<StockBasic> stockBasics =
        iStockBasicService.selectStockBasicList(StockBasic.builder().symbol(stockCode).build());
    if (CollectionUtils.isEmpty(stockBasics)) {
      return null;
    }
    StockBasic stockBasic = stockBasics.get(0);
    // 交易所 SSE上交所 SZSE深交所
    Map<String, String> header = new HashMap<>();
    header.put("referer", "http://finance.sina.com.cn");
    String result =
        HttpUtils.sendGet(
            "SSE".equals(stockBasic.getExchange())
                ? (URL + "sh" + stockCode)
                : (URL + "sz" + stockCode),
            null,
            header);
    String[] split = result.split("\"");
    String[] split1 = split[1].split(",");
    StockNow build =
        StockNow.builder()
            .symbol(stockCode)
            .name(stockBasic.getName())
            .open(new BigDecimal(split1[1]).setScale(2, RoundingMode.HALF_UP))
            .preClose(new BigDecimal(split1[2]).setScale(2, RoundingMode.HALF_UP))
            .now(new BigDecimal(split1[3]).setScale(2, RoundingMode.HALF_UP))
            .high(new BigDecimal(split1[4]).setScale(2, RoundingMode.HALF_UP))
            .low(new BigDecimal(split1[5]).setScale(2, RoundingMode.HALF_UP))
            .vol(new BigDecimal(split1[8]).divide(new BigDecimal(100), 0, RoundingMode.HALF_UP))
            .amount(
                new BigDecimal(split1[9]).divide(new BigDecimal(10000), 0, RoundingMode.HALF_UP))
            .buy1Vol(
                new BigDecimal(split1[10])
                    .divide(new BigDecimal(10000), 0, RoundingMode.HALF_UP)
                    .intValue())
            .buy1Amount(new BigDecimal(split1[11]).setScale(2, RoundingMode.HALF_UP))
            .buy2Vol(
                new BigDecimal(split1[12])
                    .divide(new BigDecimal(10000), 0, RoundingMode.HALF_UP)
                    .intValue())
            .buy2Amount(new BigDecimal(split1[13]).setScale(2, RoundingMode.HALF_UP))
            .buy3Vol(
                new BigDecimal(split1[14])
                    .divide(new BigDecimal(10000), 0, RoundingMode.HALF_UP)
                    .intValue())
            .buy3Amount(new BigDecimal(split1[15]).setScale(2, RoundingMode.HALF_UP))
            .buy4Vol(
                new BigDecimal(split1[16])
                    .divide(new BigDecimal(10000), 0, RoundingMode.HALF_UP)
                    .intValue())
            .buy4Amount(new BigDecimal(split1[17]).setScale(2, RoundingMode.HALF_UP))
            .buy5Vol(
                new BigDecimal(split1[18])
                    .divide(new BigDecimal(10000), 0, RoundingMode.HALF_UP)
                    .intValue())
            .buy5Amount(new BigDecimal(split1[19]).setScale(2, RoundingMode.HALF_UP))
            .sell1Vol(
                new BigDecimal(split1[20])
                    .divide(new BigDecimal(10000), 0, RoundingMode.HALF_UP)
                    .intValue())
            .sell1Amount(new BigDecimal(split1[21]).setScale(2, RoundingMode.HALF_UP))
            .sell2Vol(
                new BigDecimal(split1[22])
                    .divide(new BigDecimal(10000), 0, RoundingMode.HALF_UP)
                    .intValue())
            .sell2Amount(new BigDecimal(split1[23]).setScale(2, RoundingMode.HALF_UP))
            .sell3Vol(
                new BigDecimal(split1[24])
                    .divide(new BigDecimal(10000), 0, RoundingMode.HALF_UP)
                    .intValue())
            .sell3Amount(new BigDecimal(split1[25]).setScale(2, RoundingMode.HALF_UP))
            .sell4Vol(
                new BigDecimal(split1[26])
                    .divide(new BigDecimal(10000), 0, RoundingMode.HALF_UP)
                    .intValue())
            .sell4Amount(new BigDecimal(split1[27]).setScale(2, RoundingMode.HALF_UP))
            .sell5Vol(
                new BigDecimal(split1[28])
                    .divide(new BigDecimal(10000), 0, RoundingMode.HALF_UP)
                    .intValue())
            .sell5Amount(new BigDecimal(split1[29]).setScale(2, RoundingMode.HALF_UP))
            .tradeDate(split1[30])
            .tradeTime(split1[31])
            .build();
    build.setChange(build.getNow().subtract(build.getPreClose()));
    build.setPctChg(
        build
            .getChange()
            .multiply(new BigDecimal(100))
            .divide(build.getPreClose(), 2, RoundingMode.HALF_UP));
    return build;
  }
}
